package com.xinqi.modules.chat.favorite.controller.app;

import com.xinqi.common.base.response.R;
import com.xinqi.common.core.utils.MessageUtils;
import com.xinqi.common.satoken.utils.LoginHelper;
import com.xinqi.modules.chat.favorite.dto.req.FavoriteTagCreateDTO;
import com.xinqi.modules.chat.favorite.dto.req.FavoriteTagDeleteDTO;
import com.xinqi.modules.chat.favorite.dto.req.FavoriteTagUpdateDTO;
import com.xinqi.modules.chat.favorite.dto.rsp.FavoriteTagResultDTO;
import com.xinqi.modules.chat.favorite.service.FavoriteTagService;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * APP -- 收藏标签
 *
 * @author: yyzhen
 * @date: 2023/03/23
 */
@Validated
@RestController
@RequestMapping("/app/favorite/tag")
@RequiredArgsConstructor
public class FavoriteTagAppController {
    private final FavoriteTagService favoriteTagService;

    /**
     * 添加收藏标签
     *
     * @Title:
     * @MethodName: insert
     * [dto]
     * @Return com.xinqi.common.base.response.R
     * @Exception
     * @Description:
     * @author: yyzhen
     * @date: 2023/3/23 13:57
     */
    @PostMapping("/add")
    public R insert(@Validated @RequestBody FavoriteTagCreateDTO dto) {
        dto.setOperationUserId(LoginHelper.getUserId());
        if (favoriteTagService.insert(dto)) {
            return R.ok(MessageUtils.message("chat.favorite.tag.add.success"));
        }
        return R.fail(MessageUtils.message("chat.favorite.tag.add.fail"));
    }

    /**
     * 修改收藏标签
     *
     * @Title:
     * @MethodName: update
     * [dto]
     * @Return com.xinqi.common.base.response.R
     * @Exception
     * @Description:
     * @author: yyzhen
     * @date: 2023/3/23 13:57
     */
    @PostMapping("/edit")
    public R update(@Validated @RequestBody FavoriteTagUpdateDTO dto) {
        dto.setOperationUserId(LoginHelper.getUserId());
        if (favoriteTagService.updateById(dto)) {
            return R.ok(MessageUtils.message("chat.favorite.tag.update.success"));
        }
        return R.fail(MessageUtils.message("chat.favorite.tag.update.fail"));
    }

    /**
     * 删除收藏标签
     *
     * @Title:
     * @MethodName: remove
     * [dto]
     * @Return com.xinqi.common.base.response.R
     * @Exception
     * @Description:
     * @author: yyzhen
     * @date: 2023/3/23 13:57
     */
    @PostMapping("/remove")
    public R remove(@Validated @RequestBody FavoriteTagDeleteDTO dto) {
        dto.setOperationUserId(LoginHelper.getUserId());
        if (favoriteTagService.deleteById(dto)) {
            return R.ok(MessageUtils.message("chat.favorite.tag.remove.success"));
        }
        return R.fail(MessageUtils.message("chat.favorite.tag.remove.fail"));
    }

    /**
     * 用户收藏标签列表
     *
     * @Title:
     * @MethodName: list
     * []
     * @Return com.xinqi.common.base.response.R<java.util.List < com.xinqi.modules.chat.favorite.dto.rsp.FavoriteTagResultDTO>>
     * @Exception
     * @Description:
     * @author: yyzhen
     * @date: 2023/3/23 14:14
     */
    @GetMapping("/list")
    public R<List<FavoriteTagResultDTO>> list(@RequestParam(name = "keywords", required = false) String keywords) {
        return R.ok(favoriteTagService.listByUserId(keywords, LoginHelper.getUserId()));
    }

    /**
     * 收藏标签详情
     *
     * @Title:
     * @MethodName: info
     * [id]
     * @Return com.xinqi.common.base.response.R<com.xinqi.modules.chat.favorite.dto.rsp.FavoriteTagResultDTO>
     * @Exception
     * @Description:
     * @author: yyzhen
     * @date: 2023/3/23 14:14
     */
    @GetMapping("/info/{id}")
    public R<FavoriteTagResultDTO> info(@NotNull(message = "{chat.favorite.tag.id.not.blank}") @PathVariable("id") Long id) {
        return R.ok(favoriteTagService.findById(id));
    }

}
